package top.lyc829.mymusic.ext

import android.util.Log
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.NavHostFragment
import com.lzx.starrysky.utils.showToast

/**
 * @Author lyc
 * @create 2021-07-05 16:58
 */
@Suppress("UNCHECKED_CAST")
fun <F : Fragment> AppCompatActivity.getTopFragment(fragmentClass: Class<F>): F? {
    val navHostFragment = this.supportFragmentManager.fragments.first() as NavHostFragment

    navHostFragment.childFragmentManager.fragments.forEach {
        if (fragmentClass.isAssignableFrom(it.javaClass)) {
            return it as F
        }
    }
    return null
}

fun Fragment.showToast(msg: String = "") {
    activity?.let {
        if (!it.isFinishing) {
            it.showToast(msg)
        }
    }
}